﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows;
using OODCRM.Common;
using System.Data.Linq;

namespace OODCRM
{
    public partial class MainWindow : RibbonWindow
    {

        private void btnCat_tabThemChienDichDialog_Click(object sender, RoutedEventArgs e)
        {
            switch (dicDialogMode[CRMDialog.ThemChienDich])
            {
                case DialogMode.Them:
                    if (dicDialogData.ContainsKey(CRMDialog.ThemChienDich)
                        && dicDialogData[CRMDialog.ThemChienDich] != null)
                    {
                        var item = dicDialogData[CRMDialog.ThemChienDich] as CHIENDICH;
                        if (item == null) break;

                        // cap nhat du lieu
                        try
                        {
                          
                            dataContext.CHIENDICHes.InsertOnSubmit(item);
                            dataContext.SubmitChanges();

                            if (item != null)
                                listChienDich.Add(item);
                        }
                        catch (Exception ex)
                        {

                            //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                            MessageBox.Show(ex.Message);
                            dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                        }
                    }
                    break;
                case DialogMode.Sua:
                    {
                        var item = dicDialogData[CRMDialog.ThemChienDich] as CHIENDICH;
                        if (item == null) break;

                        // update du lieu ko bind dc
                        //var chuquan = cbxChuQuan_tabThemCDialog.SelectedItem as CHIENDICH;
                        //if (chuquan != null)
                        //    item.MaChuQuan = chuquan.MaHieu;
                        var chusohuu = cbxChuSoHuu_tabThemChienDichDialog.SelectedItem as User;
                        if (chusohuu != null)
                            item.DOITUONG.User = chusohuu;

                        // cap nhat du lieu
                        try
                        {
                            dataContext.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                            MessageBox.Show(ex.Message);
                            dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                        }
                    }
                    break;               

                default:
                    break;
            }
            tabChienDich.IsSelected = true;
            
        }

        private void btnHuyBo_tabThemChienDichDialog_Click(object sender, RoutedEventArgs e)
        {
            //dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
            if (dicDialogMode[CRMDialog.ThemChienDich] == DialogMode.Sua)
            {
                var item = dicDialogData[CRMDialog.ThemChienDich] as CHIENDICH;
                if (item != null)
                    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, item);

                int index = listChienDich.IndexOf(item);
                listChienDich.RemoveAt(index);
                listChienDich.Insert(index, item);
            }

            tabChienDich.IsSelected = true;
        }
    }
}
